﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Xml;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Configuration;

namespace GateTester
{
    public partial class testresult : System.Web.UI.Page
    {
        long RequestId;
        protected void Page_Load(object sender, EventArgs e)
        {
            кнЗанести.Visible = false;
            if (IsPostBack)
            {
                DetailsView1.DataBind();
                if (((DataRowView)DetailsView1.DataItem).Row["Status"].ToString().Equals("OK"))
                    кнЗанести.Visible = true;
                return;
            }
            if (PreviousPage == null)
            {
                Response.Redirect("Default.aspx");
                return;
            }
            string script = Request.Params["tbScript"].Trim().TrimStart('/','\\');
            string запрос;            
            StringBuilder запросБуф=new StringBuilder();
                if(Request.Params["ddlMethod"]=="XML")
                {
                    string[] допПараметры = Request.Params["tbAuxParam"].Split(new Char[]{'=','\r','\n'}, StringSplitOptions.RemoveEmptyEntries);
                    TextWriter stringWriter = new StringWriter();
                    XmlWriterSettings settings = new XmlWriterSettings();                    
                    settings.OmitXmlDeclaration = true;
                    using (XmlWriter writer = XmlWriter.Create(stringWriter,settings))
                    {
                        writer.WriteStartElement("CMP");
                        writer.WriteAttributeString("Ver", Request.Params["Ver"]);
                        writer.WriteStartElement("Payment");
                        int i = 0;
                        for (; Request.Params.Keys[i] != "Payment_id"; i++) ;
                        for (; Request.Params.Keys[i] != "tbAuxParam"; i++)
                            if (Request.Params[i] != "")
                                writer.WriteAttributeString(Request.Params.Keys[i], Request.Params[i]);
                        for (int j = 0; j < допПараметры.Length;)
                            writer.WriteAttributeString(допПараметры[j++], допПараметры[j++]);
                        writer.WriteEndElement();
                        writer.Flush();                     
                    }
                    запрос = stringWriter.ToString();
                }
                else
                {
                    int i = 0;
                    string[] допПараметры = Request.Params["tbAuxParam"].Split(new Char[]{}, StringSplitOptions.RemoveEmptyEntries);
                    string delim=Request.Params["ddlMethod"].Equals("GET") ? "&" : "\r\n";
                    for (; Request.Params.Keys[i] != "Ver"; i++) ;
                    for (; Request.Params.Keys[i] != "tbAuxParam"; i++)
                        if(Request.Params[i]!="")
                            запросБуф.AppendFormat("{0}={1}"+delim, Request.Params.Keys[i], Request.Params[i]);                                        
                    запрос = Server.UrlPathEncode(запросБуф.Append(String.Join(delim,допПараметры)).ToString().TrimEnd('&'));
                }
            ConnectionStringSettingsCollection connectionStrings =
                WebConfigurationManager.ConnectionStrings as ConnectionStringSettingsCollection;
            ConnectionStringSettings cs = connectionStrings["sms_dConnectionString"];            
            using (SqlConnection conn = new SqlConnection(cs.ConnectionString))
            {
                SqlCommand comm = new SqlCommand("ProvTest.usp_AddRequest", conn);
                comm.CommandType = CommandType.StoredProcedure;                
                comm.Parameters.Add("@Protocol", SqlDbType.VarChar, 5).Value = Request.Params["ddlProtocol"];
                comm.Parameters.Add("@Method", SqlDbType.VarChar, 4).Value = Request.Params["ddlMethod"].Equals("GET")?"get":"post";
                comm.Parameters.Add("@Login", SqlDbType.VarChar, 24).Value = Request.Params["tbLogin"];
                comm.Parameters.Add("@Password", SqlDbType.VarChar, 32).Value = Request.Params["tbPassword"];
                comm.Parameters.Add("@Host", SqlDbType.VarChar, 48).Value = Request.Params["tbHost"];
                comm.Parameters.Add("@Port", SqlDbType.VarChar, 5).Value = Request.Params["tbPort"];
                comm.Parameters.Add("@Script", SqlDbType.VarChar, 128).Value = Request.Params["tbScript"];
                comm.Parameters.Add("@Query", SqlDbType.VarChar,1024).Value = Request.Params["ddlMethod"].Equals("GET")?запрос:"";
                comm.Parameters.Add("@PostData", SqlDbType.VarChar, 1024).Value = Request.Params["ddlMethod"].Equals("GET")?"":запрос;
                comm.Parameters.Add("@ContentType", SqlDbType.VarChar, 36).Value = Request.Params["ddlContType"];
                comm.Parameters.Add("@Provider", SqlDbType.VarChar, 50).Value = Request.Params["tbProvider"];
                comm.Parameters.Add("@Id", SqlDbType.BigInt).Direction = ParameterDirection.Output;
                conn.Open();
                comm.ExecuteNonQuery();
                RequestId = (long)comm.Parameters["@Id"].Value;
                lblID.Text = RequestId.ToString();
            }
        }
    }
}
